﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public  class FangXing_DAL
    {
        public FangXing_DAL(Context context)
        {
            Context = context;
        }

        public Context Context { get; }

        /// <summary>
        /// 房型添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public int FangXingAdd(FangXing_Model m)
        {
            var m1=Context.FangXing_Model.FirstOrDefault(x=>x.FXName == m.FXName);
            if(m1 != null)
            {
                return -1;  //房型名称重复
            }
            else
            {
                Context.FangXing_Model.Add(m);
                return Context.SaveChanges();
            }
        }
        /// <summary>
        /// 房型列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="Count"></param>
        /// <param name="Count2"></param>
        /// <returns></returns>
        public List<FangXing_Model> FangXingShow(int index,int size,out int Count,out int Count2)
        {
             var m=Context.FangXing_Model.Where(x=>x.FXIsDeleted==false).ToList();
            Count = m.Count;
            Count2 = (int)Math.Ceiling(Count * 1.0 / size);
            return m.OrderBy(x=>x.FXId).Skip((index-1)*size).Take(size).ToList();
        }
        /// <summary>
        /// 房型删除
        /// </summary>
        /// <param name="FXId"></param>
        /// <returns></returns>
        public int FangXingShan(int FXId)
        {
            var m=Context.FangXing_Model.FirstOrDefault(x=> x.FXId == FXId);
            m.FXIsDeleted = true;
            return Context.SaveChanges();   
        }
        /// <summary>
        /// 房型状态
        /// </summary>
        /// <param name="FXId"></param>
        /// <param name="FXZT"></param>
        /// <returns></returns>
        public int FangXingZT(int FXId,bool FXZT)
        {
            var m = Context.FangXing_Model.FirstOrDefault(x => x.FXId == FXId);
            m.FXZT = FXZT;
            return Context.SaveChanges();
            
        }
        /// <summary>
        /// 房型反填
        /// </summary>
        /// <param name="FXId"></param>
        /// <returns></returns>
        public FangXing_Model FangXingFan(int FXId)
        {
            var m = Context.FangXing_Model.FirstOrDefault(x => x.FXId == FXId);
            return m;
        }

        /// <summary>
        /// 房型修改
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public int FangXingXiouGai(FangXing_Model m)
        {
            var m1 = Context.FangXing_Model.FirstOrDefault(x => x.FXId == m.FXId);
           m1.FXName= m.FXName;
            m1.FXNum = m.FXNum;
            m1.MSPrice = m.MSPrice;
            m1.FXZT = m.FXZT;
            m1.FHId = m.FHId;   
           return Context.SaveChanges();
            
        }

    }
}
